﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using Project.Engine.Utilities;
using System.Web.SessionState;
using Project.Module.DAO.CMSBL;

namespace Project.Module.Controller
{
    public class CMSLoginController
    {
    }

    public partial class HomeController
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CMSLoginAccount(string name, string pass)
        {
            // login
            bool check = (new UserBL()).CMSLoginUser(name, pass);
            if (check == true)
            {
                CMSLogin(name);
                // return
                return Json(new { result = 0 }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                // return
                return Json(new { result = 1 }, JsonRequestBehavior.AllowGet);  // sai user hoac pass
            }
        }

        public void CMSLogin(string name)
        {
            Session["username_ad"] = name;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult CMSRegAccount(string name, string email, string pass)
        {
            if (MailUtil.CheckValidMail(email) == true)
            {
                // check account
                bool check = (new UserBL()).CMSCheckAccountUser(name);
                if (check == true)
                {
                    // insert
                    (new UserBL()).CMSInsertUser(name, email, pass);
                }
                else
                {
                    // return
                    return Json(new { result = 2 }, JsonRequestBehavior.AllowGet);  // username da co nguoi dang ki
                }

                // return
                return Json(new { result = 0 }, JsonRequestBehavior.AllowGet);  // dang ki thanh cong
            }

            // return
            return Json(new { result = 1 }, JsonRequestBehavior.AllowGet); // email ko dung dinh dang
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult CMSEditAccount(int id, string email, string pass)
        {
            if (MailUtil.CheckValidMail(email) == true)
            {
                // update
                (new UserBL()).CMSUpdateUser(id, email, pass);
                // return
                return Json(new { result = 0 }, JsonRequestBehavior.AllowGet);  // dang ki thanh cong
            }

            // return
            return Json(new { result = 1 }, JsonRequestBehavior.AllowGet); // email ko dung dinh dang
        }

        public ActionResult CMSLogout()
        {
            if (Session["username"] != null && ((string)(Session["username"])).Length > 0)
            {
                Session["username"] = String.Empty;
            }

            return View("/Views/Backend/Home/Login.cshtml");
        }

        public ActionResult DeleleUser(int id) {
            (new UserBL()).DeleteUser(id);
            return Redirect("/admin/quan-ly-nhom-quyen");
        }
    }
}
